//
//  ALButton.m
//  Wutuojia
//
//  Created by Work on 15/7/19.
//  Copyright (c) 2015年 Work. All rights reserved.
//

#import "ALButton.h"
//#import "ALView.h"
//
#import "UIView+Frame.h"
//#import "ALComAction.h"
#import "UIButton+WebCache.h"
#import "Masonry.h"
#import "ALUtils.h"

@implementation ALButton{
	UIViewContentMode _contentModel;
}
- (id)init{
	self=[super init];
	if (self) {
		_contentModel=UIViewContentModeScaleAspectFill;
		[self setBackgroundColor:[UIColor clearColor]];
		[self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
	}
	return self;
}
- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		_contentModel=UIViewContentModeScaleAspectFill;
		[self setBackgroundColor:[UIColor clearColor]];
		[self addTarget:self action:@selector(click:)
	   forControlEvents:UIControlEventTouchUpInside];
	}
	return self;
}

-(void)setFrame:(CGRect)frame{
	[super setFrame:frame];
	_contentModel=UIViewContentModeScaleAspectFill;
}

//-(void)setFrame:(CGRect)frame andTit:(NSString *)tit andGoImgView:(NSString *)imgName{
//    [super setFrame:frame];
//    [self setBackgroundColor:[UIColor clearColor]];
//    ALView *vipDetailView_4=[[ALView alloc]
//                             initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
//    [vipDetailView_4 setBackgroundColor:[UIColor whiteColor]];
//    [vipDetailView_4 setCustemViewWithType:BothLine_type
//                           andTopLineColor:RGB_Line_Gray
//                        andBottomLineColor:RGB_Line_Gray];
//    [vipDetailView_4 setTag:104];
//    [self addSubview:vipDetailView_4];
//
//    UIView *lineView = [[UIView alloc]
//                        initWithFrame:CGRectMake(0, 40, kScreenWidth, 1)];
//    [lineView setBackgroundColor:RGB_Line_Gray];
//    [vipDetailView_4 addSubview:lineView];
//
//    ALLabel *vipTitle_4 = [[ALLabel alloc]
//                           initWithFrame:CGRectMake(10, 1, 150, 38)
//                           BoldFont:14
//                           BGColor:RGB_BG_Clear
//                           FontColor:RGB_Font_First_Title];
//    [vipTitle_4 setText:tit];
//    [vipDetailView_4 addSubview:vipTitle_4];
//
//    [self setImage:IMG_WITH_NAME(imgName) forState:UIControlStateNormal];
//    [self setImageEdgeInsets:UIEdgeInsetsMake(0, 285, 0, 0)];
//
//    [self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//
//
//    //    OcnButton *vipGoBtn_4 = [OcnButton buttonWithType:UIButtonTypeCustom];
//    //    [vipGoBtn_4 setTag:204];
//    //    [vipGoBtn_4 setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
//    //    [vipGoBtn_4 setImage:Img(@"btn_next@2x", @"png") forState:UIControlStateNormal];
//    //    [vipGoBtn_4 setImageEdgeInsets:UIEdgeInsetsMake(0, 285, 0, 0)];
//    //    [vipGoBtn_4 setTheBtnClickBlock:^(id sender){
//    //    }];
//    //    [vipDetailView_4 addSubview:vipGoBtn_4];
//}
-(void)click:(id)sender{
    if (self.theBtnClickBlock) {
        self.theBtnClickBlock(sender);
    }
}

-(void)setAlignment{
    [self setImageEdgeInsets:UIEdgeInsetsZero];
    [self setTitleEdgeInsets:UIEdgeInsetsZero];

    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

    [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.centerX.equalTo(self.mas_centerX);
    }];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(self.height-self.titleLabel.height, -self.imageView.width, 0, 0)];
}
-(void)setAlignment:(AlignmentEnum)type{
    [self setImageEdgeInsets:UIEdgeInsetsZero];
    [self setTitleEdgeInsets:UIEdgeInsetsZero];
    [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    switch (type) {
        case ALImgUp_type:{
            [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top);
                make.centerX.equalTo(self.mas_centerX);
            }];
            if (self.imageView.width==50) {
                [self setTitleEdgeInsets:UIEdgeInsetsMake(self.height-self.titleLabel.height, -self.imageView.width-100/2, 0, 0)];
            }else{
                [self setTitleEdgeInsets:UIEdgeInsetsMake(self.height-self.titleLabel.height, -self.imageView.width, 0, 0)];
            }
            self.titleLabel.centerX=self.imageView.centerX;
            break;
        }

        case ALImgDown_type:{
            [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(self.titleLabel.height);
                make.centerX.equalTo(self.mas_centerX);
            }];

            [self setTitleEdgeInsets:UIEdgeInsetsMake(-self.imageView.height, -self.imageView.width, 0, 0)];

            break;
        }
        case ALImgRight_type:{
//            self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.right.equalTo(self.mas_right)
//            }
        }
            break;
        default:
            break;
    }
}
//- (void)correctWidth{
//    CGSize size=[ALComAction getSizeByStr:self.titleLabel.text
//                                  andFont:self.titleLabel.font];
//    if (!isAfterIphone6) {
//        [self setWidth:size.width+15];
//    }else{
//        [self setWidth:size.width+25];
//    }
//}
//
//- (void)setImageWithURLStr:(NSString *)url
//       placeholderImageStr:(NSString *)placeholder
//                  forState:(UIControlState)theState{
//    [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//}
//
//- (void)setImageWithURLStr:(NSString *)url
//                  forState:(UIControlState)theState{
//    [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//}
//- (void)setImageWithURLStr:(NSString *)url type:(ALImgEnum)type{
//    NSString *midPath=nil;
//    switch (type) {
//        case small:
//        {
//            midPath=@"/pic/small";
//            break;
//        }
//        case middle:
//        {
//            midPath=@"/pic/middle";
//            break;
//        }
//        case large:
//        {
//            midPath=@"/pic/large";
//            break;
//        }
//        default:
//            break;
//    }
//    if (![url containsString:@"http"]) {
//        [[ALComAction sharedInstance] configModelCallback:^(ALConfigModel *model) {
//            [self setImageWithURLCommon:[NSURL URLWitALtring:[NSString stringWithFormat:@"%@%@%@",[model dynSite],midPath,url]]];
//        }];
//    }else{
//        [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//    }
//
//}
//- (void)setImageWithURLStr:(NSString *)url size:(CGSize)size{
//    if (![url containsString:@"http"]) {
//        [[ALComAction sharedInstance] configModelCallback:^(ALConfigModel *model) {
//            NSString *midPath=[NSString stringWithFormat:@"/pic/sq_%dx%d",[@(size.width) integerValue]*2,[@(size.height) integerValue]*2];
//            NSString *reUrlStr=[NSString stringWithFormat:@"%@%@%@",[model dynSite],midPath,url];
//            [self setImageWithURLStr:reUrlStr type:middle];
//        }];
//    }else{
//        [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//    }
//}
//- (void)setImageWithURLStr:(NSString *)url type:(ALImgEnum)type tit:(NSString *)tit{
//    if (![url containsString:@"http"]) {
//        [[ALComAction sharedInstance] configModelCallback:^(ALConfigModel *model) {
//            [self setImageWithURLStr:url type:middle];
//            id sender= [self viewWithTag:1234];
//            if ([sender isKindOfClass:[ALLabel class]]&&[sender tag]==1234) {
//                ALLabel *titLbl=sender;
//                [titLbl setText:tit];
//            }else{
//                ALLabel *titLbl=[[ALLabel alloc] init];
//                [titLbl setTextColor:[UIColor whiteColor]];
//                [titLbl setText:tit];
//                [titLbl setTextAlignment:NSTextAlignmentCenter];
//                [titLbl setTag:1234];
//                [self addSubview:titLbl];
//                [titLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.top.equalTo(@0);
//                    make.width.equalTo(self.mas_width);
//                    make.height.equalTo(self.mas_height);
//                    make.centerX.equalTo(self.mas_centerX);
//                    make.centerY.equalTo(self.mas_centerY);
//                }];
//            }
//        }];
//    }else{
//        [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//    }
//
//}
//- (void)setImageWithURLStr:(NSString *)url size:(CGSize)size tit:(NSString *)tit{
//    if (![url containsString:@"http"]) {
//        [[ALComAction sharedInstance] configModelCallback:^(ALConfigModel *model) {
//            NSString *midPath=[NSString stringWithFormat:@"/pic/sq_%dx%d",[@(size.width) integerValue]*2,[@(size.height) integerValue]*2];
//            NSString *reUrlStr=[NSString stringWithFormat:@"%@%@%@",[model dynSite],midPath,url];
//            [self setImageWithURLCommon:[NSURL URLWitALtring:reUrlStr]];
//            id sender= [self viewWithTag:1234];
//            if ([sender isKindOfClass:[ALLabel class]]&&[sender tag]==1234) {
//                ALLabel *titLbl=sender;
//                [titLbl setText:tit];
//            }else{
//                ALLabel *titLbl=[[ALLabel alloc] init];
//                [titLbl setTextColor:[UIColor whiteColor]];
//                [titLbl setText:tit];
//                [titLbl setTextAlignment:NSTextAlignmentCenter];
//                [titLbl setTag:1234];
//                [self addSubview:titLbl];
//                [titLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.top.equalTo(@0);
//                    make.width.equalTo(self.mas_width);
//                    make.height.equalTo(self.mas_height);
//                    make.centerX.equalTo(self.mas_centerX);
//                    make.centerY.equalTo(self.mas_centerY);
//                }];
//            }
//        }];
//    }else{
//        [self setImageWithURLCommon:[NSURL URLWitALtring:url]];
//    }
//}
//- (void)setImageWithURLStr:(NSString *)url size:(CGSize)size tit:(NSString *)tit andModel:(UIViewContentMode)contentModel{
//    _contentModel=contentModel;
//    [self setImageWithURLStr:url size:size tit:tit];
//}
//- (void)setImageWithURLStr:(NSString *)url type:(ALImgEnum)type andModel:(UIViewContentMode)contentModel{
//    _contentModel=contentModel;
//    [self setImageWithURLStr:url type:type];
//}
//- (void)setImageWithURLCommon:(NSURL *)url{
//    //默认
//    self.contentMode =  UIViewContentModeCenter;
//    [self setContentScaleFactor:[[UIScreen mainScreen] scale]];
//    self.clipsToBounds  = YES;
//
//    UIImage *placeImg=[ALImage imageNamed:@"placeBigRec"];
//    [self sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:placeImg completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        [self setImage:nil forState:UIControlStateNormal];
//        [self setBackgroundImage:image forState:UIControlStateNormal];
//        self.imageView.contentMode =  _contentModel;
//    }];
//}
- (void)setTitAndImg:(NSString *)tit{
    [self setTitle:tit forState:UIControlStateNormal];
    CGFloat imgWidth=self.imageView.image.size.width;
    CGFloat titWidth=[ALUtils getSizeByStr:tit andFont:self.titleLabel.font].width;
    CGFloat minSep=5;
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, titWidth+5+((self.width-(imgWidth+titWidth+minSep))/2), 0, 0)];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgWidth+((self.width-(imgWidth+titWidth+minSep))/2), 0, 0)];
}
@end
